﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NoteExpress;

namespace neut.FolderAnalysis
{
    public partial class FolderTree : Form
    {
        public FolderTree()
        {
            InitializeComponent();
        }
        public TreeNodeCollection Nodes
        {
            get { return treeView1.Nodes; }
        }
        public int Sum
        {
            get;
            set;
        }
        public string FolderName
        {
            get
            {
                return this.Text;
            }
            set
            {
                this.Text = value;
            }
        }

        public INEItem[] Items
        { get; set; }

        public void AddNode(INEItem item, bool bStartFolder)
        {
            var foldersstr = item.GetPaths();
            Array.Sort(foldersstr);

            foreach (var path in foldersstr)
            {
                if (bStartFolder && path.IndexOf("*") > -1) continue;
                var node = treeView1.Nodes;
                var folders = path.Split(new string[]{"\\"}, StringSplitOptions.RemoveEmptyEntries);
                var tmp0 = "\\" + "References\\";
                for (var i = 1; i < folders.Length; i++)
                {
                    tmp0 += folders[i]+"\\";
                    var tnode = node.IndexOfKey(folders[i]);

                    if (tnode < 0)
                    {
                        var tmp = new TreeNodeEx(folders[i],tmp0);
                        tmp.Add(item, tmp0==path);
                        var n = node.Add(tmp);
                        node = node[n].Nodes;
                    }
                    else
                    {
                        ((TreeNodeEx)node[tnode]).Add(item, tmp0 == path);
                        node = node[tnode].Nodes;
                    }
                }


                this.ResumeLayout(false);
                /*
                var str = "";
                foreach (var tt in path.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries))
                {                    
                    str += "\\"+tt;
                    bool bOnly = path != (str+"\\") ? false : true;

                    TreeNode tnode = null;
                    foreach (TreeNode i in node)
                    {
                        if (i.Text == tt)
                            tnode = i;
                    }
                    if (tnode == null)
                    {
                        var tmp = new TreeNodeEx(str.ToNEFolder());
                        tmp.Add(item,bOnly);
                        var n = node.Add(tmp);
                        node = node[n].Nodes;
                    }
                    else
                    {
                        tnode.ToEx().Add(item,bOnly);
                        node = tnode.Nodes;
                    }
                }
                 * */
            }

        }
        void ReText(TreeNodeCollection node)
        {
            foreach (TreeNodeEx t in node)
            {
                t.ReText(Sum);
                foreach (var tmp in t.ItemsOnly)
                    t.ToolTipText = t.ToolTipText + "<"+tmp.Key+">\n";
                ReText(t.Nodes);
            }
        }

        private void FolderTree_Load(object sender, EventArgs e)
        {
            ReText(treeView1.Nodes);
            treeView1.ExpandAll();
        }

        private void goToFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView1.SelectedNode.ToEx().Folder.ToNEFolder().Activate();
        }

        private void outputToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            var ne = new NEHost2();
            var tmp = ne.Libraries.SelectReferenceFolder("请选择或建立文件夹", ne.Internals.CurrentDBID);
            if (tmp == null)
                return;
            var node=treeView1.SelectedNode.ToEx().GetItems();
            foreach (var t in node)
            {
                tmp.LinkItem(t);
            }
            this.Show();
        }

        private void createSubFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ne = new NEHost2();
            
            var node = treeView1.SelectedNode.ToEx();
            var tt=ne.Libraries.ActiveFolder;
            var tmp = tt.Add(node.Name);
            tt.Activate();
            foreach (var t in node.GetItems())
            {
                tmp.LinkItem(t);
            }
        }

        private void createSubFolerOnlyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ne = new NEHost2();

            var node = treeView1.SelectedNode.ToEx();
            var tt = ne.Libraries.ActiveFolder;
            var tmp = tt.Add(node.Name);
            tt.Activate();
            foreach (var t in node.GetItemsOnly())
            {
                tmp.LinkItem(t);
            }
        }

        private void outputActiveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ne = new NEHost2();
            var node = treeView1.SelectedNode.ToEx();
            foreach (var t in node.GetItems())
            {
                ne.Libraries.ActiveFolder.LinkItem(t);
            }
        }

        void AddFolder(INEFolder folder,TreeNode n)
        {
            var node = n.ToEx();
            var tt = folder;
            var tmp = tt.Add("&[" + node.Name + "]");
            tt.Activate();
            foreach (var t in node.GetItemsOnly())
            {
                tmp.LinkItem(t);
            }
            foreach (TreeNode t in node.Nodes)
            {
                AddFolder(tmp, t);
            }
        }

        private void addFolderAllSubFoldersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ne = new NEHost2();
            var t = ne.Libraries.ActiveFolder;
            AddFolder(t, treeView1.SelectedNode);
            t.Activate();
        }
    }



    public class TreeNodeEx : TreeNode
    {
        static IDictionary<string, INEFolder> dic = new Dictionary<string, INEFolder>();
        public static void Reset()
        {
            dic = new Dictionary<string, INEFolder>();
        }
        public Dictionary<string, string> Items = new Dictionary<string, string>();

        public Dictionary<string, string> ItemsOnly = new Dictionary<string, string>();
      

        public TreeNodeEx(string name,string folder)
        {
            Folder = folder;
            Name= name;
            Text = name;
        }

        public void Add(INEItem item,bool bOnly)
        {
            if (item.Values["title"].StartsWith("{")) return;
            if (!Items.Keys.Contains(item.Values["ID"])) Items.Add(item.Values["ID"], Folder);
            if (bOnly && !ItemsOnly.Keys.Contains(item.Values["ID"])) ItemsOnly.Add(item.Values["ID"], Folder);
        }

        public void ReText(int sum)
        {
            Text = Name + " <" + (Items.Count / (double)sum * 100).ToString("##")+"%" + "=" + Items.Count + "; "
                + (ItemsOnly.Count / (double)sum * 100).ToString("##") + "%" + "=" + ItemsOnly.Count + ">";
        }

        public string Folder
        {
            get;set;
        }


        public INEItem[] GetItems()
        {
            var rt = new List<INEItem>();
            foreach (var t in Items)
            {
                if (dic.Keys.Contains(t.Value))
                    rt.Add(dic[Folder].FindItemFromID(t.Key));
                else
                {
                    var tmp = Folder.ToNEFolder();

                    dic.Add(Folder, tmp);
                    rt.Add(tmp.FindItemFromID(t.Key));
                }
            }
            return rt.ToArray();
        }

        public INEItem[] GetItemsOnly()
        {
            var rt = new List<INEItem>();
            foreach (var t in ItemsOnly)
            {
                if (dic.Keys.Contains(t.Value))
                    rt.Add(dic[Folder].FindItemFromID(t.Key));
                else
                {
                    var tmp = Folder.ToNEFolder();

                    dic.Add(Folder, tmp);
                    rt.Add(tmp.FindItemFromID(t.Key));
                }
            }
            return rt.ToArray();
        } 


    }


    static class Extension
    {
        public static TreeNodeEx ToEx(this TreeNode t)
        {
            return (TreeNodeEx)t;
        }
    }

}
